﻿#include "pch.h"

CGrpInfWrapper::CGrpInfWrapper()
{
    memset((void *)this, 0, sizeof(CGrpInfWrapper));
}

void CGrpInfWrapper::printSelf(char *captionUpper, int32_t)
{
    char caption[1024];
    sprintf(caption, "%s:%s", captionUpper, "CGrpInfWrapper");
    print4x(this, sizeof(CGrpInfWrapper), caption);
    if (this->head)
    {
        sprintf(caption, "%s:%s", captionUpper, "CGrpInfWrapper.head");
        print4x(this->head, this->len * this->size, caption);
    }
}

// sub_1001D0F0
CGrpInfWrapper *CGrpInfWrapper::init(TDLLTPRGINF *grpInf)
{
    this->init((LPVOID)grpInf->head, grpInf->len, grpInf->num);
    return this;
};

// sub_1001DDC0
CGrpInfWrapper *CGrpInfWrapper::init(LPVOID head, int32_t len, int32_t *num)
{
    this->head = head;
    this->size = 72;
    this->len = len;
    this->tail = 0;
    this->needFreeHead = false;
    this->num = num;
    return this;
};

// sub_1001EDA5
int32_t CGrpInfWrapper::clean()
{
    int32_t result;
    int32_t i;

    for (i = 0;; ++i)
    {
        result = i;
        if (i >= *(this->num))
        {
            break;
        }
        TDLLGROUPListItem *pGroup = (TDLLGROUPListItem *)this->head;
        pGroup[i].Area = 0.0;
        pGroup[i].Height = 0.0;
        pGroup[i].AreaPer = 0.0;
        pGroup[i].Conc = 0.0;
    }
    return result;
};

// sub_1001F215
int32_t CGrpInfWrapper::calcGroupAggregate(TDLLResultListItem *pDLLResult, int32_t num)
{
    int32_t j;
    int32_t i;
    int32_t result;
    TDLLGROUPListItem *pGroup = (TDLLGROUPListItem *)this->head;

    result = false;
    if (!*this->num || !num)
    {
        return false;
    }
    for (i = 0; i < *this->num; ++i)
    {
        pGroup[i].Area = 0.0;
        pGroup[i].Height = 0.0;
        pGroup[i].AreaPer = 0.0;
        pGroup[i].Conc = 0.0;
        for (j = 0; j < num; ++j)
        {
            if (this->checkRTime(i, pDLLResult[j].RTime))
            {
                pGroup[i].Area = pGroup[i].Area + pDLLResult[j].Area;
                pGroup[i].Height = pDLLResult[j].Height + pGroup[i].Height;
                pGroup[i].AreaPer = pGroup[i].AreaPer + pDLLResult[j].AreaPer;
                pGroup[i].Conc = pGroup[i].Conc + pDLLResult[j].Conc;
                if (!result)
                {
                    result = true;
                }
            }
        }
    }
    return result;
};

// sub_1001F0C2
int32_t CGrpInfWrapper::checkRTime(int32_t idx, float rtime)
{
    TDLLGROUPListItem *pGroup = (TDLLGROUPListItem *)this->head;
    if (pGroup[idx].TrLower < 0.0 || pGroup[idx].TrUpper < 1.0e-30)
    {
        return 0;
    }
    if (pGroup[idx].TrUpper <= (double)pGroup[idx].TrLower)
    {
        return pGroup[idx].TrUpper >= fabs(rtime - pGroup[idx].TrLower);
    }
    return rtime > (double)pGroup[idx].TrLower && rtime < (double)pGroup[idx].TrUpper;
}
